package com.ruoyi.safe.controller.api;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.safe.domain.BizProjectCheckRecord;
import com.ruoyi.safe.domain.BizSecurityLog;
import com.ruoyi.safe.domain.dto.*;
import com.ruoyi.safe.service.IBizProjectCheckRecordService;
import com.ruoyi.safe.service.IBizSecurityLogService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 安全日志app端
 * 
 * @author zhangyu
 * @date 2024-02-01
 */
@RestController
@RequestMapping("/api/security/log")
public class SecurityLogController extends BaseController
{
    @Autowired
    private IBizSecurityLogService bizSecurityLogService;
    @Autowired
    private IBizProjectCheckRecordService bizProjectCheckRecordService;

    @Autowired
    ISysUserService sysUserService;

    /**
     * 查询安全日志列表
     */
    @PostMapping("/list")
    public R<Object> list(@RequestBody BizSecurityLogAppDTO bizSecurityLogAppDTO)
    {
        getSysUser(bizSecurityLogAppDTO.getUserName());
        BizSecurityLog bizSecurityLog = new BizSecurityLog();
        BeanUtils.copyProperties(bizSecurityLogAppDTO, bizSecurityLog);
        startPage(bizSecurityLogAppDTO.getPageNum(), bizSecurityLogAppDTO.getPageSize());
        List<BizSecurityLog> list = bizSecurityLogService.selectBizSecurityLogList(bizSecurityLog);
        return R.ok(new PageInfo<>(list));
    }

    /**
     * 查询日志视图--本月数据列表
     */
    @PostMapping("/logView")
    public R<Object> logView(@RequestBody BizSecurityLogAppDTO bizSecurityLogAppDTO)
    {
        SysUser sysUser = getSysUser(bizSecurityLogAppDTO.getUserName());
        BizSecurityLog bizSecurityLog = new BizSecurityLog();
        BeanUtils.copyProperties(bizSecurityLogAppDTO, bizSecurityLog);
        startPage(bizSecurityLogAppDTO.getPageNum(), bizSecurityLogAppDTO.getPageSize());
        List<BizSecurityLog> list = bizSecurityLogService.selectLogList(bizSecurityLog, sysUser);
        return R.ok(new PageInfo<>(list));
    }

    /**
     * 获取安全日志详细信息
     */
    @PostMapping(value = "/info")
    public AjaxResult getInfo(@RequestBody @Validated EntityIdAppDTO entityIdAppDTO)
    {
        AjaxResult ajaxResult = new AjaxResult();
        List<BizProjectCheckRecord> list = new ArrayList<>();
        BizSecurityLog bizSecurityLog = bizSecurityLogService.selectBizSecurityLogById(entityIdAppDTO.getId());
        if(!CollectionUtils.isEmpty(bizSecurityLog.getRecordIds())){
            bizSecurityLog.getRecordIds().stream().forEach(item -> {
                BizProjectCheckRecord bizProjectCheckRecord = bizProjectCheckRecordService.selectBizRecordById(item);
                list.add(bizProjectCheckRecord);
                ajaxResult.put("bizProjectCheckRecord",list);
            });
        }
        ajaxResult.put("bizSecurityLog",bizSecurityLog);
        return success(ajaxResult);
    }

    /**
     * 新增安全日志
     */
    @PostMapping("/add")
    public AjaxResult add(@RequestBody BizSecurityLogAppDTO bizSecurityLogAppDTO)
    {
        SysUser sysUser = getSysUser(bizSecurityLogAppDTO.getUserName());
        BizSecurityLog bizSecurityLog = new BizSecurityLog();
        BeanUtils.copyProperties(bizSecurityLogAppDTO, bizSecurityLog);
        return toAjax(bizSecurityLogService.insertBizSecurityLog(bizSecurityLog, sysUser));
    }

    /**
     * 修改安全日志
     */
    @PostMapping("/edit")
    @Transactional
    public AjaxResult edit(@RequestBody BizSecurityLogAppDTO bizSecurityLogAppDTO)
    {
        SysUser sysUser = getSysUser(bizSecurityLogAppDTO.getUserName());
        BizSecurityLog bizSecurityLog = new BizSecurityLog();
        BeanUtils.copyProperties(bizSecurityLogAppDTO, bizSecurityLog);

        return toAjax(bizSecurityLogService.updateBizSecurityLog(bizSecurityLog, sysUser));
    }

    /**
     * 删除安全日志
     */
	@PostMapping("/delete")
    public AjaxResult remove(@RequestBody EntityIdsAppDTO entityIdsAppDTO)
    {
        getSysUser(entityIdsAppDTO.getUserName());
        return toAjax(bizSecurityLogService.deleteBizSecurityLogByIds(entityIdsAppDTO.getIds()));
    }

    /**
     * 查询项目下的安全日志
     * @param entityIdAppDTO
     * @return
     */
    @PostMapping("/getListByProjectId")
    public R<Object> getListByProjectId(@RequestBody EntityIdAppDTO entityIdAppDTO) {
        getSysUser(entityIdAppDTO.getUserName());
        startPage(entityIdAppDTO.getPageNum(), entityIdAppDTO.getPageSize());
        List<BizSecurityLog> bizSecurityLogList
                = bizSecurityLogService.selectListByProjectId(entityIdAppDTO.getProjectId());
        return R.ok(new PageInfo<>(bizSecurityLogList));
    }

    /**
     * 查询未添加的检查记录
     * @param projectCheckRecordDTO
     * @return
     */
    @PostMapping("/unRecordList")
    public R<Object> unRecordList(@RequestBody BizProjectCheckRecordDTO projectCheckRecordDTO){
        getSysUser(projectCheckRecordDTO.getUserName());
        startPage(projectCheckRecordDTO.getPageNum(), projectCheckRecordDTO.getPageSize());

        BizProjectCheckRecord projectCheckRecord = new BizProjectCheckRecord();
        BeanUtils.copyProperties(projectCheckRecordDTO, projectCheckRecord);

        List<BizProjectCheckRecord> list =bizProjectCheckRecordService.selectUnRecordList(projectCheckRecord);
        return R.ok(new PageInfo<>(list));
    }

    /**
     * 批量新增检查记录
     * @param bizRecordIdsAppDTO
     * @return
     */
    @PostMapping("/insertRecords")
    public AjaxResult insertRecords(@RequestBody BizRecordIdsAppDTO bizRecordIdsAppDTO) {
        getSysUser(bizRecordIdsAppDTO.getUserName());

        BizRecordIdsDTO bizRecordIdsDTO = new BizRecordIdsDTO();
        BeanUtils.copyProperties(bizRecordIdsAppDTO, bizRecordIdsDTO);

        return toAjax(bizSecurityLogService.insertRecords(bizRecordIdsDTO));
    }

    /**
     * 查询已添加的数据
     * @param bizRecordIdsAppDTO
     * @return
     */
    @PostMapping("/allRecordList")
    public R<Object> allRecordList(@RequestBody BizRecordIdsAppDTO bizRecordIdsAppDTO){
        getSysUser(bizRecordIdsAppDTO.getUserName());
        startPage(bizRecordIdsAppDTO.getPageNum(), bizRecordIdsAppDTO.getPageSize());
        List<BizProjectCheckRecord> list
                = bizProjectCheckRecordService.selectAllRecordList(bizRecordIdsAppDTO.getCreateTime());
        return R.ok(new PageInfo<>(list));
    }

    private static void startPage(Integer bizSecurityLogAppDTO, Integer bizSecurityLogAppDTO1) {
        Integer pageNum = bizSecurityLogAppDTO;
        Integer pageSize = bizSecurityLogAppDTO1;
        if (!Objects.isNull(pageNum) && !Objects.isNull(pageSize)) {
            PageUtils.startPage(pageNum, pageSize);
        }
    }

    private SysUser getSysUser(String userName) {
        SysUser sysUser = sysUserService.selectUserByUserName(userName);
        if (sysUser == null) {
            throw new ServiceException(StringUtils.format("用户信息不存在：{}", userName));
        }

        return sysUser;
    }
}
